﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity; // needed for lambda includes



using System.Text;

namespace TheCup.ApiControllers
{
    public class ScoreController : ApiController
    {
        TheCupContext ctx = new TheCupContext();

        public IEnumerable<Scorecard> Get()
        {
            List<Scorecard> e = new List<Scorecard>();
            e = (from i in ctx.Scorecards
                 select i).ToList<Scorecard>();
            return e;
        }

        public IEnumerable<Scorecard> GetScorecardsForPlayer(int Id, int year)
        {
            List<Scorecard> e = new List<Scorecard>();
            e = (from item in ctx.Scorecards.Include(tee => tee.Tee).Include(r => r.Round).Include(pl => pl.Player).Include(rc => rc.Round.Course).Include(re => re.Round.Event)
                 where item.Player.Id.Equals(Id) && item.Round.DateEntry.Year.Equals(year) 
                 select item).ToList<Scorecard>();
            foreach (Scorecard r in e)
            {
                //r.Round.PrepareScorecardsForRankingLists();
            }
            return e;
        }

        public Scorecard Get(int id)
        {
            Scorecard e = new Scorecard();
            e = (from item in ctx.Scorecards.Include(tee => tee.Tee).Include(r => r.Round).Include(pl => pl.Player).Include(rc => rc.Round.Course).Include(re => re.Round.Event)
                 where item.Id == id
                 select item).FirstOrDefault();
            return e;
        }

        // POST api/<controller>
        [Authorize]
        public HttpResponseMessage Post([FromBody]Scorecard value)
        {
            if (ModelState.IsValid)
            {
                ctx.Scorecards.Add(value);
                ctx.SaveChanges();
                
                return Request.CreateResponse(HttpStatusCode.OK, value.Id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "bad request");
            }
        }

        // PUT api/<controller>/5
        [Authorize]
        public HttpResponseMessage Put(int id, [FromBody]Scorecard value)
        {
            if (ModelState.IsValid && value != null)
            {
                Scorecard oldCard = ctx.Scorecards.Find(id);

                for (int i = 1; i < 19; i++)
                {
                    oldCard.SetStrokesForHole(i, value.GetStrokesForHole(i));
                    oldCard.SetPuttsForHole(i, value.GetPuttsForHole(i));
                }
                ctx.SaveChanges();

                return Request.CreateResponse(HttpStatusCode.OK, "success");
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "bad request");
            }
        }

        // DELETE api/<controller>/5
        [Authorize]
        public HttpResponseMessage Delete(int id)
        {
            if (id == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "no such id");
            }
            else
            {
                Scorecard scorecard = 
                    (from sc in ctx.Scorecards
                    where sc.Id.Equals(id)
                    select sc).First();
                
                ctx.Scorecards.Remove(scorecard);
                ctx.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK);
            }           
        }
    }
}